﻿USE [iKBSystem]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[WeiboUser]'))
DROP TABLE [dbo].[WeiboUser]
GO

CREATE TABLE [dbo].[WeiboUser](
	[Key] [uniqueidentifier] NOT NULL DEFAULT NEWID(),
	[DomainScopeKey] [uniqueidentifier] NOT NULL,
	[ThirdPartyIdentity] [varchar](150) NOT NULL,
	[DisplayName] [nvarchar](MAX) NOT NULL,
	[GivenName] [nvarchar](MAX) NULL,--My description
	[Gender] [int] NOT NULL DEFAULT 0,
	[Birthday] [datetime] NULL,
	[Email] [varchar](MAX) NULL,
	[Blood] [nvarchar](20) NULL,	
	[ArticleCount] [int] NOT NULL DEFAULT 0,
	[FollowCount] [int] NOT NULL DEFAULT 0,
	[FansCount] [int] NOT NULL DEFAULT 0,
	[Description] [nvarchar](MAX) NULL,--Owner's self description
	[OtherInfo] [nvarchar](MAX) NULL,--TEMP
	[UserTags] [nvarchar](MAX) NULL,
	[AvatorKey] [uniqueidentifier] NOT NULL,
	[CreatedStamp] [datetime] NOT NULL,
	[LastUpdatedStamp] [datetime] NOT NULL,
 CONSTRAINT [PK_WeiboUser] PRIMARY KEY CLUSTERED 
([Key] ASC)ON [PRIMARY])
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[UserAvator]'))
DROP TABLE [dbo].[UserAvator]
GO

CREATE TABLE [dbo].[UserAvator](
	[Key] [uniqueidentifier] NOT NULL DEFAULT NEWID(),
	[UserKey] [uniqueidentifier] NOT NULL,
	[FileKey] [uniqueidentifier] NOT NULL,
 CONSTRAINT [PK_UserAvator] PRIMARY KEY CLUSTERED 
([Key] ASC)ON [PRIMARY])

GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[WeiboImage]'))
DROP TABLE [dbo].[WeiboImage]
GO

CREATE TABLE [dbo].[WeiboImage](
	[Key] [uniqueidentifier] NOT NULL DEFAULT NEWID(),
	[OwnerKey] [uniqueidentifier] NOT NULL,
	[FileKey] [uniqueidentifier] NOT NULL,
 CONSTRAINT [PK_WeiboImage] PRIMARY KEY CLUSTERED 
([Key] ASC)ON [PRIMARY])

GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[WeiboArticle]'))
DROP TABLE [dbo].[WeiboArticle]
GO

CREATE TABLE [dbo].[WeiboArticle](
	[Key] [uniqueidentifier] NOT NULL DEFAULT NEWID(),
	[SystemId] [varchar](128) NOT NULL,
	[DomainScopeKey] [uniqueidentifier] NOT NULL,
	[ContentType] [int] NOT NULL DEFAULT 1, --Is comment or article, etcs
	[PublishedFor] [uniqueidentifier] NULL,--If comment, for article, If article, for parent node, If Original, NULL
	[OriginalSource] [uniqueidentifier] NOT NULL,--Original Article
	[Owner] [uniqueidentifier] NOT NULL,
	[Content] [nvarchar](MAX) NOT NULL,
	[VideoUrl] [nvarchar](MAX) NULL,--If has video
	[ImageUrl] [nvarchar](MAX) NULL,--If has images
	[GeoInfo] [nvarchar](MAX) NULL,--If is GIS
	[ForwardCount] [int] NOT NULL DEFAULT 0,
	[CommentsCount] [int] NOT NULL DEFAULT 0,
	[Agent] [nvarchar](50) NOT NULL DEFAULT '',
	[AnalyzeFlag] [bit] NOT NULL DEFAULT 0,
	[CreatedStamp] [datetime] NOT NULL,
	[LastUpdatedStamp] [datetime] NOT NULL,
 CONSTRAINT [PK_WeiboArticle] PRIMARY KEY CLUSTERED 
([Key] ASC)ON [PRIMARY])

GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ArticleImages]'))
DROP TABLE [dbo].[ArticleImages]
GO

CREATE TABLE [dbo].[ArticleImages](
	[Key] [uniqueidentifier] NOT NULL DEFAULT NEWID(),
	[MD5] [varchar](150) NOT NULL,
	[OriginalSourceUrl] [varchar](300) NOT NULL,
	[Height] [int] NOT NULL DEFAULT 0,
	[Width] [int] NOT NULL DEFAULT 0,
	[CreatedStamp] [datetime] NOT NULL,
 CONSTRAINT [PK_ArticleImages] PRIMARY KEY CLUSTERED 
([Key] ASC)ON [PRIMARY])

GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Agent]'))
DROP TABLE [dbo].[Agent]
GO

CREATE TABLE [dbo].[Agent](
	[Key] [uniqueidentifier] NOT NULL DEFAULT NEWID(),
	[ArticleKey] [uniqueidentifier] NOT NULL,
	[MD5] [varchar](150) NOT NULL,
	[OriginalUrl] [varchar](300) NOT NULL,
	[FilePath] [varchar](200) NOT NULL,
	[CreatedStamp] [datetime] NOT NULL,
 CONSTRAINT [PK_Agent] PRIMARY KEY CLUSTERED 
([Key] ASC)ON [PRIMARY])

GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GeoInfo]'))
DROP TABLE [dbo].[ArticleImages]
GO

CREATE TABLE [dbo].[ArticleImages](
	[Key] [uniqueidentifier] NOT NULL DEFAULT NEWID(),
	[ArticleKey] [uniqueidentifier] NOT NULL,
	[MD5] [varchar](150) NOT NULL,
	[OriginalUrl] [varchar](300) NOT NULL,
	[FilePath] [varchar](200) NOT NULL,
	[HasThumb] [bit] NOT NULL DEFAULT 0,
	[CreatedStamp] [datetime] NOT NULL,
 CONSTRAINT [PK_ArticleImages] PRIMARY KEY CLUSTERED 
([Key] ASC)ON [PRIMARY])

GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[WeiboUserRelationship]'))
DROP TABLE [dbo].[WeiboUserRelationship]
GO

--[UserKey] follows [TargetUserKey]
CREATE TABLE [dbo].[WeiboUserRelationship](
	[UserKey] [uniqueidentifier] NOT NULL,
	[TargetUserKey] [uniqueidentifier] NOT NULL,
	[IsDeleted] [bit] NOT NULL DEFAULT 0,
	[CreatedStamp] [datetime] NOT NULL,
	[LastUpdatedStamp] [datetime] NOT NULL,
 CONSTRAINT [PK_WeiboUserRelationship] PRIMARY KEY CLUSTERED 
([UserKey],[TargetUserKey] ASC)ON [PRIMARY])
GO